<?php

namespace App\Http\Controllers\Api\v1;

use App\Http\Controllers\Api\BaseController;
use App\Http\Resources\Record;
use App\Record as recordModel;
use App\Repositories\RecordRepository;
use Auth;
use Illuminate\Http\Request;

class RecordController extends BaseController {

	protected $model;

	public function __construct(RecordRepository $record) {
		parent::__construct();
		$this->model = $record;
	}

	public function store(Request $request) {
		$this->validate($request, $this->rules());
		$list = $this->getResource($this->model->store(array_merge(['user_id' => Auth::user()->id], $request->all())))->type('complete');
		return $this->response->withSuccess($list);
	}

	public function update(Request $request, $id) {
		$this->validate($request, $this->rules());
		$list = $this->getResource($this->model->update($id, $request->all()))->type('complete');
		return $this->response->withSuccess($list);
	}

	public function changeStatus(Request $request, $id) {
		$this->validate($request, [
			'status' => 'required|boolean',
		]);
		$this->model = $this->model->withOutStatus();
		$this->model->update($id, $request->only('status'));
		return $this->response->withNoContent();
	}

	public function destroy($id) {

	}

	private function rules() {
		return [
			'note_id' => 'sometimes|required|numeric',
			'title' => 'required|max:18',
			'content' => 'required',
		];
	}

	protected function getResource(recordModel $collection) {
		return new Record($collection);
	}
}
